﻿Imports System.IO
Imports System.Linq
Imports System.Xml.Linq

Public Class mainForm
   
    Dim listPerson As List(Of person) = New List(Of person)
    Dim listResult As List(Of person)

    Private Sub mainForm_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
        readFile()
        DataGridView1.AutoGenerateColumns = False
    End Sub

    Private Sub readFile()
        Dim sr As New IO.StreamReader("CSV-Employees.txt")
        Dim line As String = ""
        While Not sr.EndOfStream
            Dim per As New person
            Dim person As String()
            line = sr.ReadLine()
            person = line.Split(","c)
            per.firstname = person(0)
            per.lastname = person(1)
            per.extension = person(2)
            per.code = getCode(per.firstname, per.lastname)
            listPerson.Add(per)
        End While
    End Sub

    Private Function getCode(firstname As String, lastname As String)
        Dim num1 As Integer = getCharCode(firstname(0))
        Dim num2 As Integer = getCharCode(lastname(0))
        Dim num3 As Integer = getCharCode(lastname(1))
        Dim num4 As Integer = getCharCode(lastname(2))
        Dim code As String = String.Empty
        code = num1.ToString() + num2.ToString() + num3.ToString() + num4.ToString()
        Return code
    End Function

    Private Function getCharCode(c As Char)
        Select Case c
            Case "a"c To "c"c
                Return 2
            Case "A"c To "C"c
                Return 2
            Case "d"c To "f"c
                Return 3
            Case "D"c To "D"c
                Return 3
            Case "g"c To "i"c
                Return 4
            Case "G"c To "G"c
                Return 4
            Case "j"c To "k"c
                Return 5
            Case "J"c To "K"c
                Return 5
            Case "m"c To "o"c
                Return 6
            Case "M"c To "O"c
                Return 6
            Case "p"c To "s"c
                Return 7
            Case "P"c To "S"c
                Return 7
            Case "t"c To "v"c
                Return 8
            Case "T"c To "V"c
                Return 8
            Case "w"c To "z"c
                Return 9
            Case "W"c To "Z"c
                Return 9

        End Select

    End Function

    Private Sub btnDisplay_Click(sender As System.Object, e As System.EventArgs) Handles btnDisplay.Click
        listResult = New List(Of person)

        Dim query = From per In listPerson
                    Where per.code = lbCode.Text
                    Select per
        For Each per In query
            listResult.Add(per)
        Next

        Dim bindingSource As New BindingSource
        bindingSource.DataSource = listResult
        DataGridView1.DataSource = bindingSource
    End Sub

    Private Sub btnOne_Click(sender As System.Object, e As System.EventArgs) Handles btnOne.Click
        lbCode.Text = lbCode.Text + "1"
    End Sub

    Private Sub btnTwo_Click(sender As System.Object, e As System.EventArgs) Handles btnTwo.Click
        lbCode.Text = lbCode.Text + "2"
    End Sub

    Private Sub btnThree_Click(sender As System.Object, e As System.EventArgs) Handles btnThree.Click
        lbCode.Text = lbCode.Text + "3"
    End Sub

    Private Sub btnFour_Click(sender As System.Object, e As System.EventArgs) Handles btnFour.Click
        lbCode.Text = lbCode.Text + "4"
    End Sub

    Private Sub btnFive_Click(sender As System.Object, e As System.EventArgs) Handles btnFive.Click
        lbCode.Text = lbCode.Text + "5"
    End Sub

    Private Sub btnSix_Click(sender As System.Object, e As System.EventArgs) Handles btnSix.Click
        lbCode.Text = lbCode.Text + "6"
    End Sub

    Private Sub btnSeven_Click(sender As System.Object, e As System.EventArgs) Handles btnSeven.Click
        lbCode.Text = lbCode.Text + "7"
    End Sub

    Private Sub btnEight_Click(sender As System.Object, e As System.EventArgs) Handles btnEight.Click
        lbCode.Text = lbCode.Text + "8"
    End Sub

    Private Sub btnNine_Click(sender As System.Object, e As System.EventArgs) Handles btnNine.Click
        lbCode.Text = lbCode.Text + "9"
    End Sub

    Private Sub btnStar_Click(sender As System.Object, e As System.EventArgs) Handles btnStar.Click
        lbCode.Text = lbCode.Text + "*"
    End Sub

    Private Sub btnZero_Click(sender As System.Object, e As System.EventArgs) Handles btnZero.Click
        lbCode.Text = lbCode.Text + "0"
    End Sub

    Private Sub btnSharp_Click(sender As System.Object, e As System.EventArgs) Handles btnSharp.Click
        lbCode.Text = lbCode.Text + "#"
    End Sub

    Private Sub btnLookUp_Click(sender As System.Object, e As System.EventArgs) Handles btnLookUp.Click
        lbCode.Text = ""
        listResult = New List(Of person)
        Dim bindingSource As New BindingSource
        bindingSource.DataSource = listResult
        DataGridView1.DataSource = bindingSource
    End Sub

    Private Sub btnExit_Click(sender As System.Object, e As System.EventArgs) Handles btnExit.Click
        Me.Close()
    End Sub

    Private Sub btnInstructions_Click(sender As System.Object, e As System.EventArgs) Handles btnInstructions.Click
        Dim str As String = "Enter the first letters of the person's last name followed by the first letter of the person's first name."
        MessageBox.Show(str, "Instructions")
    End Sub
End Class
